﻿#ifndef AUDIO_H
#define AUDIO_H

#include <QObject>
#include <QFile>
#include <QAudioInput>
#include <QAudioOutput>
#include <QThread>
#include <QDebug>
#include "ringbuffer.h"
#include "soundtouchex.h"

enum PlayMode
{
    None = 0,
    PcmFile,
    Device
};

class FileThread;
class DeviceThread;

class AudioPlayer : public QThread
{
public:
    explicit AudioPlayer(QObject *parent = 0);
    ~AudioPlayer();

    void setAudioParm(PlayMode mode, const QString &url, int sampleRate, int sampleSize, int channel);

    void startPlayPcmFile();
    void startPlayDevice();

    void startPlay();
    void stopPlay();

    bool isRunState();

protected:
    void run();

private:
    bool isRun;
    bool runState;

    QAudioOutput *audioOutput;
    PlayMode mode;
    QString playUrl;

    int sampleRate;
    int sampleSize;
    int channel;
};

class FileThread : public QThread
{
public:
    explicit FileThread(QObject *parent = 0);
    ~FileThread();

protected:
    void run();

public:
    bool isRun;
    bool readFinish;

    QFile *pcmFile;
    RingBuffer *ringBuffer;
    int pcmSize;
};

class DeviceThread : public QThread
{
public:
    explicit DeviceThread(QObject *parent = 0);
    ~DeviceThread();

protected:
    void run();

public:
    bool isRun;
    bool readFinish;

    SoundTouchEx *soundTouchEx;
    QAudioInput *audioInput;
    RingBuffer *ringBuffer;
    QString playUrl;
    int sampleRate;
    int sampleSize;
    int channel;
};

#endif // AUDIO_H
